<?php


namespace Biz\module\Classes\wiki;


use Doraemon\model\TopicRelation;
use Doraemon\tools\Log;
use Doraemon\tools\ParticipleKeyword;
use Doraemon\model\Category;
use Biz\cache\wiki\TopicWiki as cTopicWiki;

class TopicWiki
{
    /**
     * 获取相关话题的所有分类名称
     * @param array $params
     * @return array
     */
    public static function getCateNames($topicId, $topicTitle): array
    {
        $topic_expire_time = 3600 * 24 * 10;
        $cateNames = [];
        try {
            //查询用户设置的分词
            $topicNateNames = TopicRelation::get('key_words', ['topic_activity_id' => $topicId]);
            $topicNateNames = $topicNateNames ? explode(',', $topicNateNames) : [];
            //1.redis取
            $cateNamesRedis = cTopicWiki::getCateNameById($topicId);
            //redis不存在  可能存储的是空数组
            if (!$cateNamesRedis && !is_array($cateNamesRedis)) {
                //获取分词
                $title = ParticipleKeyword::getParticipleKeyword($topicTitle);
                $words = $title ? explode(',', $title) : [];
                $cateNames = self::getAllCateNames($words);
                //redis存储话题
                cTopicWiki::setCateNameById($topicId, $cateNames, $topic_expire_time);
            } else {
                //redis 存在
                $cateNames = $cateNamesRedis;
            }
            $cateNames = array_merge($cateNames, $topicNateNames);
        } catch (\Exception $e) {
            Log::info("根据分词获取分类", date('Y-m-d-H-i-s') . "获取话题分类错误-错误信息：" . $e->getMessage() . "参数:" . json_encode([$topicId,$topicTitle]) . "返回值：" . json_encode($cateNames));
        }

        return $cateNames;

    }

    /**
     * 获取所有子集集合
     * @param $categories @所有分类集合
     * @param int $id
     * @return array
     */
    public static function getSubs($categories, int $id): array
    {
        $subs = [];
        foreach ($categories as $item) {
            if ($item['father_id'] == $id) {
                $subs[] = $item;
                $subs = array_merge($subs, self::getSubs($categories, $item['id']));
            }
        }
        return $subs;
    }


    /**
     * 获取所有分词中文分类名称-（本身->子集）
     * @param $words @分词
     * @return array
     */
    public static function getAllCateNames($words): array
    {
        $where['status'] = Category::STATUS_USING;
        //获取商品类集合
        $categories = Category::selectByWhere($where);

        //获取符合标准商品
        $where['cate_name'] = $words;
        //查找到所有父类id
        $cateInfo = Category::selectByWhere($where, ['id', 'cate_name']);
        $names = [];
        if ($cateInfo) {
            //第一层cate_name
            $first_names = array_column($cateInfo, 'cate_name');

            //遍历查询所有父类子集分类
            $new = [];
            foreach ($cateInfo as $key => $v) {
                $new[] = array_column(self::getSubs($categories, $v['id']), 'cate_name');
            }

            //没有子类分类
            if (!$new) {
                $names = $first_names;
            } else {
                $newNames = [];
                //判断是否是一维数组
                if (count($new) != count($new, 1)) {
                    foreach ($new as $val) {
                        foreach ($val as $item) {
                            $newNames[] = $item;
                        }
                    }
                }
                $names = array_merge($first_names, $newNames);
            }
        }

        return $names;
    }
}